#include <57header.h>

/* Usage: ./03_mmap_upper filename */
int main(int argc, char *argv[]){
    ARGS_CHECK(argc, 2);
    int fd = open(argv[1], O_RDWR);
    ERROR_CHECK(fd, -1, "open");

    // 调整一下文件大小
    //int ret = ftruncate(fd, 11);
    //ERROR_CHECK(ret, -1, "ftruncate");
    struct stat sb;
    //int ret = stat(argv[1], &sb);
    //ERROR_CHECK(ret, -1, "stat");
    int ret = fstat(fd, &sb);
    ERROR_CHECK(ret, -1, "fstat");
    long size = sb.st_size;

    char* p = (char*)mmap(NULL, size, PROT_READ | PROT_WRITE,
         MAP_SHARED, fd, 0);
    ERROR_CHECK(p, MAP_FAILED, "mmap");

    // 利用指针操作内存块就是操作文件
    for(int i = 0; i < size; i++){
        p[i] = toupper(p[i]); // 读的过程中直接覆盖
    }

    close(fd);
    return 0;
}

